<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
$user_groupType = $auth['role_type'];
$user_group = $auth['role'];
$userLevel = $auth['userlevel_id'];
$userPositionID = $auth['position_id'];
$userName = $auth['firstname'] . " " . $auth['lastname'];
$getPosition = $db->query("SELECT * FROM tbposition where id = '$userPositionID'","row");
$timezone = "Asia/Manila";
//if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
//date("Y-m-d  H:i:s")
if(isset($_POST['action'])){
    if($_POST['action']=="sendMessage"){
        $dataID = $_POST['dataID'];
        $Msubject = $_POST['Msubject'];
        $Mmessage = $_POST['Mmessage'];
        $split = explode(",",$dataID);
        $insert = array("title"=>$Msubject,
                        "m_recepient_id"=>$dataID.",".$user_id,
                        "m_recepient"=>$split[0],
                        "message"=>$Mmessage,
                        "sender"=>$user_id,
                        "user_read"=>0,
                        "date"=>date("Y-m-d  H:i:s"));
        $db->insert("tbsend_message",$insert);
        echo "Message was successfully send.";
    }elseif($_POST['action']=="updateMessage"){
        // Update Message if they seen
        $dataID = $_POST['dataID']; // Message ID
        $update = array("user_read"=>1);
        $con = array("id"=>$dataID);
        $db->update("tbsend_message",$update,$con);
    }elseif($_POST['action']=="viewMessage"){
        $dataID = $_POST['dataID']; // Message ID
        $getMessage = $db->query("SELECT * FROM tbsend_message WHERE id='$dataID'","row");
        $messageID = $getMessage['id'];
        $userID = $getMessage['sender'];
        $user = $db->query("SELECT * FROM tbuser WHERE id='$userID'","row");
        echo '<div class="headMessage">
                <label class="pull-left" style="margin-left: 10px;">'.$user['firstname']. " " .$user['lastname'].'</label>
                  <a href="#" class="close pull-right"><li class="icon-remove"></li></a> 
               </div> 
               <div class="data" style="padding:0px 20px 20px 20px;">
                    <div id="loadMessageReply" style="float: left;border: 1px solid #ddd;padding: 5px;height: 390px;width: 100%;">
                        <div class="userNewMessage" style="float:left;overflow-y: auto;height: 390px;width:100%;margin-bottom:10px;">
                            '.myAvatar($user['extension'],$userID,"30","30","none").'
                            <div class="commentPost">
                                <div class="userComment">
                                '.$user['firstname']. " " .$user['lastname'].'
                                </div>
                                '.$getMessage['message'].'
                                <br>
                                <label class="fontGray timeago" style="font-size:10px;" title="'.$getMessage['date'].'"></label>
                            </div>';
                            // Get Replied Message
                            $getReplied = $db->query("SELECT * FROM tbreply_message WHERE messageID='$messageID'","array");
                            foreach($getReplied as $reply){
                                $userID = $reply['userID'];
                                $user = $db->query("SELECT * FROM tbuser WHERE id='$userID'","row");
                                echo myAvatar($user['extension'],$userID,"30","30","none");
                                echo '
                                    <div class="commentPost">
                                        <div class="userComment">
                                        '.$user['firstname']. " " .$user['lastname'].'
                                        </div>
                                        '.$reply['messageReply'].'
                                        <br>
                                        <label class="fontGray timeago" style="font-size:10px;" title="'.$reply['date'].'"></label>
                                    </div>';
                                
                            }
        echo '                    
                        </div>
                    </div>
                    <div style="float: left;border: 1px solid #ddd;border-top:none;padding: 5px;height: 55px;width: 100%;">
                        <textarea class="input-comment replyMessage reply" data-id="'.$dataID.'" wrap="off"></textarea>
                    </div>
               </div>';
    }elseif($_POST['action']=="replyMessage"){
        $dataID = $_POST['dataID'];
        $replyVal = $_POST['replyVal'];
        $insert = array("messageReply"=>$replyVal,
                        "userID"=>$auth['id'],
                        "messageID"=>$dataID,
                        "user_read"=>0,
                        "date"=>date("Y-m-d  H:i:s"));
        $db->insert("tbreply_message",$insert);
        echo '<div class="userNewMessage" id="message_'.$dataID.'" style="float:left;width:100%;overflow:auto;margin-bottom:10px;">
                '.myAvatar($auth['extension'],$auth['id'],"30","30","none").'
                <div class="commentPost">
                    <div class="userComment">
                    '.$auth['firstname']. " " .$auth['lastname'].'
                    </div>
                    '.$replyVal.'
                    <br>
                    <label class="fontGray timeago" style="font-size:10px;" title="'.date("Y-m-d  H:i:s").'"></label>
                </div>
            </div>';
    }elseif($_POST['action']=="deleteMyMessage"){
        $dataID = $_POST['dataID']; // User ID
        $messageID = $_POST['messageID']; //Message ID
        $splitMessageID = explode(",",$messageID);
        $splitDataID = explode(",",$dataID);
        foreach($splitMessageID as $mID){
            $getMessage = $db->query("SELECT * FROM tbsend_message WHERE id='$mID'","array");
            foreach($getMessage as $data){
                $m_recepient_id = explode(",",$data['m_recepient_id']);
                $messageDbID = $data['id'];
                foreach($splitDataID as $id){
                    foreach($m_recepient_id as $dataID){
                        if($id!=$dataID){
                            $val.= $dataID.",";
                        }
                    }
                    $update = array("m_recepient_id"=>substr($val,0,strlen($val)-1));
                    $con = array("id"=>$mID);
                    $db->update("tbsend_message",$update,$con);
                    echo "Message was successfully deleted.";
                    break;
                
                }
                
                echo "\n";
                $val = "";
            }
        }
    }elseif($_POST['action']=="loadMessage"){
        $getMessage = $db->query("SELECT * FROM tbsend_message ORDER BY id DESC","array");
        foreach($getMessage as $message){
            $split = explode(",", $message['m_recepient_id']);
            
            foreach($split as $data){
                if($data==$user_id){
                    $recipient = $message['m_recepient'];
                    $sender = $message['sender'];
                    $post = substr($message['title'], 0, 10);
                    $user = $db->query("SELECT * FROM tbuser WHERE id='$sender'","row");
                        echo '<input type="checkbox" name="singleDeleteSelectedMessage[]" data-message-id="'.$message['id'].'" data-id="'.$data.'" class="singleDeleteSelectedMessage pull-right viewMessageDeleteBox display">
                            <div class="block" id="message_'.$message['id'].'" data-id="'.$message['id'].'"';
                            if($message['user_read']=="0"){
                                echo 'style="background-color:background-color:rgb(233, 233, 233);"';
                            }
                            echo 'id="'.$message['message'].'">';
                            if($recipient==$user_id){
                                echo myAvatar($user['extension'],$sender,"30","30","none");
                            }else{
                                $userImg = $db->query("SELECT * FROM tbuser WHERE id='$recipient'","row");
                                echo myAvatar($userImg['extension'],$userImg['id'],"30","30","none");
                            }
                                    
                                
                            echo '
                                <div class="commentPost">
                                    '.$post.'
                                    
                                    <br>
                                    <label class="fontGray timeago" style="font-size:10px;" title="'.$message['date'].'"></label>
                                </div>';
                        echo '</div>';
                }
            }
        }
    }
}
?>
<script>
        jQuery("label.timeago").timeago();
    </script>